package com.adv.crm.services.impl;

import com.adv.crm.beans.*;
import com.adv.crm.dao.AccountDao;
import com.adv.crm.services.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 16978 on 2016/6/19.
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    @Transactional
    public boolean addAccount(AccountBean accountBean) {
        accountDao.insertAccount(accountBean);
        return false;
    }

    @Transactional
    public List<AccountBean> getAccountsByPage(int pageSize, int pageIndex) {
        Integer offset = (pageIndex - 1) * pageSize;
        return accountDao.getAccountsByPage(offset, pageSize);
    }

    public List<AccountBean> queryAccountsByPage(AccountQueryBean bean) {
        int pageIndex = bean.getPageIndex();
        int pageSize = bean.getPageSize();

        Integer offset = (pageIndex - 1) * pageSize;
        bean.setOffset(offset);
        if (bean.getSearchType() == 1 && bean.getSearchField() == 0) {
            return accountDao.queryAccountsByAccountIdList(bean);
        } else if (bean.getSearchType() == 1 && bean.getSearchField() == 1) {
            return accountDao.queryAccountsByAccountNameList(bean);
        } else {
            return accountDao.queryAccountsByPage(bean);
        }
    }

    @Transactional
    public WarpResponse<AccountBean> getAccountById(long id) {
        WarpResponse<AccountBean> resp = new WarpResponse<AccountBean>();
        resp.setData(accountDao.getAccountById(id));
        return resp;
    }

    public WarpResponse<Boolean> updateAccountById(AccountBean bean) {
        WarpResponse<Boolean> resp = new WarpResponse<Boolean>();
        Integer result = accountDao.updateAccountById(bean);
        if (result != null && result > 0) {
            resp.setData(true);
        } else {
            resp.setSuccess(false);
            resp.setCode(-1);
            resp.setMsg("更新失败");
            resp.setData(false);
        }
        return resp;
    }

    public PageBean getPageInfo(int pageSize, int pageIndex) {
        PageBean pageBean = new PageBean();
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        Integer total = accountDao.getAccountCount();
        return getPageCount(pageSize, pageBean, total);
    }

    public PageBean queryPageInfo(AccountQueryBean bean) {
        int pageIndex = bean.getPageIndex();
        int pageSize = bean.getPageSize();
        PageBean pageBean = new PageBean();
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        Integer total = 0;
        if (bean.getSearchType() == 1 && bean.getSearchField() == 0) {
            total = accountDao.queryAccountsByAccountIdListCount(bean);
        } else if (bean.getSearchType() == 1 && bean.getSearchField() == 1) {
            total = accountDao.queryAccountsByAccountNameListCount(bean);
        } else {
            total = accountDao.queryAccountCount(bean);
        }

        return getPageCount(pageSize, pageBean, total);
    }

    private PageBean getPageCount(int pageSize, PageBean pageBean, Integer total) {
        int count = 1;
        if (total != null) {
            count = total / pageSize;
            if (total % pageSize > 0) {
                count += 1;
            }
            pageBean.setPageCount(count);
        }
        return pageBean;
    }

}
